package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.DataAssetDao;
import com.bonc.ioc.dao.FlowRecordDao;
import com.bonc.ioc.model.DataAssetInfo;
import com.bonc.ioc.model.FlowRecordInfo;
import com.bonc.ioc.service.IFlowRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author ZhangXu
 * @date 2021/4/2 9:59
 */
@Service
public class FlowRecordServiceImpl extends BaseService implements IFlowRecordService {
    private static final Logger log = LoggerFactory.getLogger(FlowRecordServiceImpl.class);

    @Resource
    private FlowRecordDao flowRecordDao;

    @Resource
    private DataAssetDao dataAssetDao;

    @Override
    public void updateTotalFlowRecord() {
        //获取当前时间
        Date nowTime = new Date();

        //获取当前时间的数据
        HashMap<String, Object> map = new HashMap<>();
        DataAssetInfo dataAssetStatus = dataAssetDao.selectDataAssetStatus();
        map.put("dataNum", dataAssetStatus.getTotalDataNum());
        map.put("dataSize", dataAssetStatus.getTotalDataSize());
        map.put("VisitNum", dataAssetStatus.getTotalVisitNum());
        map.put("UsedNum", dataAssetStatus.getTotalUsedNum());

        //更新数据记录
        updateDailyDataRecord(map,nowTime);
        updateMonthlyDataRecord(map,nowTime);
        updateAnnualDataRecord(map,nowTime);
    }

    /**
     * 更新日度数据
     * @param map 实时数据
     * @param now 当前时间
     */
    private void updateDailyDataRecord(HashMap<String, Object> map, Date now) {
        map.put("type", 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = sdf.format(now);
        map.put("nowTime", nowTime);
        map.put("recordTime", nowTime.substring(0, 10));
        //查询是否有今天的记录
        List<FlowRecordInfo> list = flowRecordDao.selectFlowRecord(map);
        if ( list.isEmpty() ) {
            //若无，则建立今天的记录
            flowRecordDao.saveDataAssetStatus(map);
            //更新数据到昨天的记录
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = c.getTime();
            map.put("recordTime", sdf.format(yesterday).substring(0, 10));
            Integer affectedRows = flowRecordDao.updateFlowRecordByRecordTime(map);
            if (affectedRows == 0) {
                flowRecordDao.saveDataAssetStatus(map);
            }
            log.info("历史记录 更新昨天的数据");
        }else {
            //若有，则更新数据到今天的记录
            flowRecordDao.updateFlowRecordByRecordTime(map);
            log.info("历史记录 更新今天的数据");
        }
        //删除过多的记录
        List<Long> ids = flowRecordDao.selectIdByType(1);
        if (!ids.isEmpty()) {
            log.info("历史记录 待删除记录ids={}", ids.toString());
            Integer affectedRows = flowRecordDao.deleteFlowRecordByIds(ids);
            log.info("历史记录 删除记录条数={}", affectedRows);
        }
    }

    /**
     * 更新月度数据
     * @param map 实时数据
     * @param now 当前时间
     */
    private void updateMonthlyDataRecord(HashMap<String, Object> map, Date now) {
        map.put("type", 2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = sdf.format(now);
        map.put("nowTime", nowTime);
        map.put("recordTime", nowTime.substring(0, 7));
        //查询是否有本月的记录
        List<FlowRecordInfo> list = flowRecordDao.selectFlowRecord(map);
        if ( list.isEmpty() ) {
            //若无，则建立本月的记录
            flowRecordDao.saveDataAssetStatus(map);
            //更新数据到上月的记录
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.add(Calendar.MONTH, -1);
            Date lastMonth = c.getTime();
            map.put("recordTime", sdf.format(lastMonth).substring(0, 7));
            Integer affectedRows = flowRecordDao.updateFlowRecordByRecordTime(map);
            if (affectedRows == 0) {
                flowRecordDao.saveDataAssetStatus(map);
            }
            log.info("历史记录 更新上月的数据");
        }else {
            //若有，则更新数据到本月的记录
            flowRecordDao.updateFlowRecordByRecordTime(map);
            log.info("历史记录 更新本月的数据");
        }
    }

    /**
     * 更新年度数据
     * @param map 实时数据
     * @param now 当前时间
     */
    private void updateAnnualDataRecord(HashMap<String, Object> map, Date now) {
        map.put("type", 3);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = sdf.format(now);
        map.put("nowTime", nowTime);
        map.put("recordTime", nowTime.substring(0, 4));
        //查询是否有今年的记录
        List<FlowRecordInfo> list = flowRecordDao.selectFlowRecord(map);
        if ( list.isEmpty() ) {
            //若无，则建立今年的记录
            flowRecordDao.saveDataAssetStatus(map);
            //更新数据到去年的记录
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.add(Calendar.YEAR, -1);
            Date lastYear = c.getTime();
            map.put("recordTime", sdf.format(lastYear).substring(0, 4));
            Integer affectedRows = flowRecordDao.updateFlowRecordByRecordTime(map);
            if (affectedRows == 0) {
                flowRecordDao.saveDataAssetStatus(map);
            }
            log.info("历史记录 更新去年的数据");
        }else {
            //若有，则更新数据到今年的记录
            flowRecordDao.updateFlowRecordByRecordTime(map);
            log.info("历史记录 更新今年的数据");
        }
    }
}
